<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>芝士erp销售管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/resources/layui/css/layui.css" media="all"/>
    <link rel="stylesheet" href="/resources/css/public.css" media="all"/>
</head>
<body class="childrenBody">
<!--query condition start-->
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 5px;">
    <legend>查询条件</legend>
</fieldset>
<blockquote class="layui-elem-quote">
    <form action="" method="post" id="searchForm" lay-filter="searchForm" class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">客户名称</label>
                <div class="layui-input-inline">
                    <select name="customerid" id="search_customerid" lay-filter="search_customerid">
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <select name="goodsid" id="search_goodsid">
                        <option value="0">请先选供应商再选商品</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">操作员</label>
                <div class="layui-input-inline">
                    <input type="text" name="operateperson" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">开始时间</label>
                <div class="layui-input-inline">
                    <input type="tel" name="startDate" id="startDate" readonly="readonly" placeholder="年-月-日 时:分:秒"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">结束时间</label>
                <div class="layui-input-inline">
                    <input type="tel" name="endDate" id="endDate" readonly="readonly" placeholder="年-月-日 时:分:秒"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-inline">
                    <input type="text" name="remark" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: right;">
                <button type="button" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="doSearch"><span
                        class="layui-icon layui-icon-search"></span>查询
                </button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-sm"
                        onclick="javascript:document.getElementById('search_customerid').value=null"><span
                        class="layui-icon layui-icon-refresh-1"></span>重置
                </button>
            </div>
        </div>
    </form>
</blockquote>
<!--query condition end-->

<!--form start-->
<div>
    <div id="salesToolBar" style="display: none;">
        <button type="button" lay-event="addSales" class="layui-btn layui-btn-sm">
            <span class="layui-icon layui-icon-add-1"></span>添加商品销售
        </button>
    </div>

    <table class="layui-hide" id="salesTable" lay-filter="salesTable"></table>

    <div id="salesRowBar" style="display: none;">
        <button type="button" lay-event="updateSales" class="layui-btn layui-btn-sm"><span
                class="layui-icon layui-icon-edit"></span>更新
        </button>
        <button type="button" lay-event="deleteSales" class="layui-btn layui-btn-sm layui-btn-danger"><span
                class="layui-icon layui-icon-delete"></span>删除
        </button>
        <button type="button" lay-event="salesback" class="layui-btn layui-btn-sm layui-btn-warm"><span
                class="layui-icon layui-icon-log"></span>退货
        </button>
    </div>
</div>
<!--form end-->

<!--添加和修改——富文本界面的隐藏和弹出-->
<div style="display: none;padding: 5px" id="addOrUpdateDiv">
    <form action="" method="post" class="layui-form layui-form-pane" id="dataForm" lay-filter="dataForm">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">客户名称</label>
                <div class="layui-input-inline">
                    <input type="hidden" name="id">
                    <select name="customerid" id="customerid" lay-filter="customerid">
                        <option value="0">请选择客户名称</option>
                    </select>
                    <div class="mydiv" title="不可修改"
                         style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"></div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">商品名称</label>
                <div class="layui-input-inline">
                    <select name="goodsid" id="goodsid">
                        <option value="0">请选择商品名称</option>
                    </select>
                    <div class="mydiv" title="不可修改"
                         style="position:absolute;width:100%;height:100%;left:0px;top:0px;background:#fff;opacity:0;filter:alpha(opacity=0)"></div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">销售数量</label>
                    <div class="layui-input-inline">
                        <input type="hidden" name="balance">
                        <input type="text" class="layui-input" name="number" lay-verify="number"
                               placeholder="请输入商品销售数量">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">销售价格</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" name="saleprice" lay-verify="number"
                               placeholder="请输入商品销售价格">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">支付类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="paytype" value="支付宝" title="支付宝" checked>
                    <input type="radio" name="paytype" value="微信" title="微信">
                    <input type="radio" name="paytype" value="银联" title="银联">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">销售备注</label>
                <div class="layui-input-block">
                    <textarea name="remark" class="layui-textarea"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block" style="text-align: right;">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="doSubmit" id="doSubmit"><span
                            class="layui-icon layui-icon-add-1"></span>提交
                    </button>
                    <button type="reset" class="layui-btn layui-btn-warm"><span
                            class="layui-icon layui-icon-refresh-1"></span>重置
                    </button>
                </div>
            </div>
        </div>
    </form>
</div>

<!-- 退货的弹出层  开始 -->
<div id="salesbackDiv" style="display: none;padding: 5px">
    <form action="" method="post" class="layui-form layui-form-pane" id="salesbackForm" lay-filter="salesbackForm">
        <div class="layui-form-item">
            <label class="layui-form-label">退货数量</label>
            <div class="layui-input-block">
                <input type="hidden" name="id"> <!-- 进货单ID -->
                <input type="hidden" name="currentnumber" id="currentnumber"> <!-- 当前进货单的数量 -->
                <input type="text" class="layui-input" name="number" lay-verify="required|checknumber"
                       placeholder="请输入退货数量">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">退货备注</label>
            <div class="layui-input-block">
                <textarea name="remark" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: right;">
                <button type="button" class="layui-btn" lay-submit="" lay-filter="salesbackSubmit" id="salesbackSubmit">
                    <span class="layui-icon layui-icon-add-1"></span>提交
                </button>
                <button type="reset" class="layui-btn layui-btn-warm"><span
                        class="layui-icon layui-icon-refresh-1"></span>重置
                </button>
            </div>
        </div>
    </form>
</div>
<!-- 退货的弹出层  结束 -->

<script type="text/javascript" src="/resources/layui/layui.js"></script>
<script type="text/javascript" th:inline="none">
    layui.use(['jquery', 'table', 'form', 'layer', 'laydate', 'layedit', 'upload'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;
        var layedit = layui.layedit;
        var upload = layui.upload;

        //初始化时间选择器
        laydate.render({
            elem: '#startDate',
            type: 'datetime'
        });
        laydate.render({
            elem: '#endDate',
            type: 'datetime'
        });

        //加载数据
        var tableIns = table.render({
            elem: '#salesTable',
            url: '/sales/loadAllSales',
            toolbar: '#salesToolBar', //开启头部工具栏
            title: '商品进货数据表',
            height: 'full-283',
            page: true,
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: '进货ID', align: 'center', width: "80"},
                {field: 'goodsname', title: '商品名称', align: 'center', width: "150"},
                {field: 'customername', title: '客户名称', align: 'center', width: "150"},
                {field: 'size', title: '商品规格', align: 'center', width: "100"},
                {field: 'salestime', title: '销售时间', align: 'center', width: '200'},
                {field: 'balance', title: '第一次销售数量', align: 'center', width: "150"},
                {field: 'number', title: '剩余可操作数量', align: 'center', width: "150"},
                {field: 'saleprice', title: '销售价格', align: 'center', width: "100"},
                {field: 'remark', title: '备注', align: 'center', width: "200"},
                {field: 'operateperson', title: '操作员', align: 'center', width: "100"},
                {field: 'paytype', title: '支付方式', align: 'center', width: "100"},
                {fixed: 'right', title: '操作', toolbar: '#salesRowBar', align: 'center', width: "250"}
            ]],
            //done:数据渲染完的回调，你可以借此做一些其他的操作
            done: function (res, curr, count) {//处理删除最后一页的bug
                if (res.data.length == 0 && curr != 1) {
                    tableIns.reload({
                        page: {
                            curr: (curr - 1)
                        }
                    });
                }
            }
        });

        //模糊查询
        form.on("submit(doSearch)", function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        //监听数据表格顶部的工具条
        table.on("toolbar(salesTable)", function (obj) {
            switch (obj.event) {
                case 'addSales':
                    addSales();
                    break;
            }
        });
        //监听数据表格当中的行工具条
        table.on("tool(salesTable)", function (obj) {
            switch (obj.event) {
                case 'deleteSales':
                    deleteSales(obj.data);
                    break;
                case 'updateSales':
                    updateSales(obj.data);
                    break;
                case 'salesback':
                    salesback(obj.data);
                    break;
            }
        });

        var mainIndex;
        var url;

        function addSales() {
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['800px', '500px'],
                title: '发布商品进货信息',
                success: function () {
                    $("#dataForm")[0].reset();
                    url = "/sales/addSales";
                    initCustomerSelect();
                    initGoodsSelect();
                    $(".mydiv").hide();
                }
            })
        }

        function initCustomerSelect(customerid) {
            var dom = $("#goodsid");
            dom.html("");
            //加载添加商品中供应商的下拉列表
            $.get("/sales/loadAllCustomerForSelect", function (res) {
                var data = res.data;
                var dom = $("#customerid");
                var html = '<option value="0">请选择客户名称</option>'
                $.each(data, function (index, item) {
                    html += '<option value="' + item.id + '">' + item.customername + '</option>'
                })
                dom.html(html);
                //如果customerid有值就反选
                if (customerid !== undefined) {
                    dom.val(customerid);
                }
                form.render("select");
            });
        }

        function initGoodsSelect(goodsid) {
            $.get("/sales/loadGoodsByCustomerId", function (res) {
                var data = res.data;
                var dom = $("#goodsid");
                var html = '<option value="0">请选择商品名称</option>'
                $.each(data, function (index, item) {
                    html += '<option value="' + item.id + '">' + item.goodsname + '——' + item.size + '</option>'
                });
                dom.html(html);
                //如果goodsid有值就反选
                if (goodsid !== undefined) {
                    dom.val(goodsid);
                }
                form.render("select");
            })
        }

        function updateSales(data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#addOrUpdateDiv"),
                area: ['800px', '500px'],
                title: '修改商品',
                success: function () {
                    $('#dataForm')[0].reset();
                    //装载新的数据
                    form.val("dataForm", data);
                    initCustomerSelect(data.customerid);
                    initGoodsSelect(data.goodsid);
                    url = "/sales/updateSales";
                    $(".mydiv").show();
                }
            })
        }

        $("#doSubmit").click(function () {
            var data = $("#dataForm").serialize();
            if (data.split("&")[1].split("=")[1] === "" || data.split("&")[2].split("=")[1] === "") {
                //此处无需打印，lay-verify会自动弹出提示信息，当然也可以打印。
                //例如layer.msg("标题或内容为空，无法提交！")
            } else {
                $.post(url, data, function (res) {
                    if (res.code === 200) {
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                    layer.close(mainIndex);
                })
            }
        })

        function deleteSales(data) {
            layer.confirm("你确定要删除【" + data.goodsname + "】这条商品进货信息吗？", {icon: 3, title: '标题'}, function (index) {
                $.post('/sales/deleteSalesById', {id: data.id}, function (res) {
                    if (res.code === 200) {
                        tableIns.reload();
                    }
                    layer.msg(res.msg);
                })
                layer.close(index);
            })
        }

        function salesback(data) {
            mainIndex = layer.open({
                type: 1,
                content: $("#salesbackDiv"),
                area: ['800px', '500px'],
                title: '商品退货',
                success: function () {
                    $('#salesbackForm')[0].reset();
                    //装载新的数据
                    form.val("salesbackForm", {id: data.id, currentnumber: data.number});
                }
            })
        }

        //自定义验证规则
        form.verify({
            checknumber: function (value) {
                var currentnumber = parseInt($("#currentnumber").val());
                if (parseInt(value) > currentnumber || parseInt(value) < 1) {
                    return '退货数量只能在【0】-【' + currentnumber + '】之间！'
                }
            }
        })
        //退货提交
        form.on("submit(salesbackSubmit)", function (data) {
            $.post("/salesback/addSalesback", data.field, function (res) {
                if (res.code === 200) {
                    tableIns.reload();
                }
                layer.msg(res.msg);
                layer.close(mainIndex);
            })
            return false;
        });


        //加载查询条件客户的下拉列表
        $.get("/sales/loadAllCustomerForSelect", function (res) {
            var data = res.data;
            var dom = $("#search_customerid");
            var html = '<option value="0">请选择供应商</option>'
            $.each(data, function (index, item) {
                html += '<option value="' + item.id + '" id="">' + item.customername + '</option>'
            })
            dom.html(html);
            form.render("select");
        })
        var select_customerid;
        form.on('select(search_customerid)', function (data) {
            // console.log(data.elem); //获得select原始DOM对象
            // console.log(data.value); //获得被选中的值
            // console.log(data.othis); //获得美化后的DOM对象
            var select_customerid = data.value;
        })
        //加载查询条件商品名称的下拉列表
        $.get("/sales/loadGoodsByCustomerId", function (res) {
            var data = res.data;
            var dom = $("#search_goodsid");
            var html = '<option value="0">请选择商品名称</option>'
            $.each(data, function (index, item) {
                html += '<option value="' + item.id + '">' + item.goodsname + '——' + item.size + '</option>'
            })
            dom.html(html);
            form.render("select");
        })

    })
</script>

</body>
</html>